﻿using Super.Admin.Entities;
using Super.Admin.Models.Permission;
using System.Text;

namespace Super.Admin.Mappers
{
    /// <summary>
    /// PermissionMapper 类用于在 PermissionEntity 和 PermissionModel 之间进行转换。
    /// </summary>
    public static class PermissionMapper
    {
        /// <summary>
        /// 将 PermissionEntity 转换为 PermissionBaseModel。
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static PermissionBaseModel ToBaseModel(this PermissionEntity entity)
        {
            if (entity == null) return null;
            return new PermissionBaseModel
            {
                Id = entity.Id,
                Name = entity.Name,
                Type = entity.Type,
                ParentId = entity.ParentId,
                Permissionkey = entity.Permissionkey,
                Path = entity.Path,
                Icon = entity.Icon != null ? Encoding.UTF8.GetString(entity.Icon) : null,
                Sort = entity.Sort,
                IsActive = entity.IsActive,
                TypeTag = entity.Type == 0 ? new AntdUI.CellTag[] { new AntdUI.CellTag("目录", AntdUI.TTypeMini.Primary) } : entity.Type == 1 ? new AntdUI.CellTag[] { new AntdUI.CellTag("菜单", AntdUI.TTypeMini.Warn) } : entity.Type == 2 ? new AntdUI.CellTag[] { new AntdUI.CellTag("按钮", AntdUI.TTypeMini.Error) } : new AntdUI.CellTag[] { new AntdUI.CellTag("未知", AntdUI.TTypeMini.Default) },
                IsActiveTag = entity.IsActive == 0 ? new AntdUI.CellTag[] { new AntdUI.CellTag("启用", AntdUI.TTypeMini.Success) } : entity.IsActive == 1 ? new AntdUI.CellTag[] { new AntdUI.CellTag("停用", AntdUI.TTypeMini.Default) } : new AntdUI.CellTag[] { new AntdUI.CellTag("未知", AntdUI.TTypeMini.Default) }
            };
        }
        /// <summary>
        /// 将 PermissionBaseModel 转换为 PermissionEntity。
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static PermissionEntity ToEntity(this PermissionBaseModel model)
        {
            if (model == null) return null;
            return new PermissionEntity
            {
                Id = model.Id,
                Name = model.Name,
                Type = model.Type,
                ParentId = model.ParentId,
                Permissionkey = model.Permissionkey,
                Path = model.Path,
                Icon = model.Icon != null ? Encoding.UTF8.GetBytes(model.Icon) : null,
                Sort = model.Sort,
                IsActive = model.IsActive,
            };
        }
        /// <summary>
        /// 将 PermissionEntity 列表转换为 PermissionBaseModel 列表。
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static IEnumerable<PermissionBaseModel> ToBaseModels(this IEnumerable<PermissionEntity> entities)
        {
            return entities?.Select(e => e.ToBaseModel()).ToList();
        }
        /// <summary>
        /// 将 PermissionBaseModel 列表转换为 PermissionEntity 列表。
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public static IEnumerable<PermissionEntity> ToEntities(this IEnumerable<PermissionBaseModel> models)
        {
            return models?.Select(m => m.ToEntity()).ToList();
        }
    }
}
